package com.power.ding.dao.impl;

import com.alibaba.fastjson.JSON;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.ding.dao.Di_userDaoCustom;
import com.power.ding.model.Di_user;
import com.power.sifa.correction.model.Crc_person;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * 用户信息 持久层实现(hibernate)
 */

@Repository
public class Di_userDaoImpl implements Di_userDaoCustom {

    private final EntityManager entityManager;

    public Di_userDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }


    @Override
    public Di_user getUserByAdid(String adid) {
        Query query = entityManager.createNativeQuery("select a.*, b.fid, b.adid from di_user a left join di_rel_dept_correct b on a.id = b.contactuser where b.adid = ?");
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        query.setParameter(1, adid);
        Map result = (Map) query.getSingleResult();
        return JSON.parseObject(JSON.toJSONString(result), Di_user.class);
    }

    @Override
    public Di_user getUserByDeptid(String deptid) {
        Query query = entityManager.createNativeQuery("select a.*, b.fid, b.adid from di_user a left join di_rel_dept_correct b on a.id = b.contactuser where b.id = ?");
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        query.setParameter(1, deptid);
        Map result = (Map) query.getSingleResult();
        return JSON.parseObject(JSON.toJSONString(result), Di_user.class);
    }
}
